﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using yncTech.BusinessObjects;
using yncTech.Common;

public partial class Admin_Sys_Sys003a : AdminBasePage
{
    decimal? systemID,functionID;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[TSSystemMetadata.ColumnNames.SystemId] != null)
        {
            systemID = decimal.Parse(Request.QueryString[TSSystemMetadata.ColumnNames.SystemId]);
        }
        if(Request.QueryString[TSFunctionsMetadata.ColumnNames.FunctionID] != null)
        {
            functionID = decimal.Parse(Request.QueryString[TSFunctionsMetadata.ColumnNames.FunctionID]);
        }

        drpChannel.SystemID = decimal.Parse(drpSystem.SelectedValue);

        if (!IsPostBack)
        {
            switch (ActionType)
            {
                case OperationType.Add:
                    btnSave.Text = "新增";
                    drpSystem.SelectedValue = systemID.Value.ToString();
                    drpChannel.SystemID = systemID.Value;
                    drpChannel.SelectedValue = functionID.Value.ToString();
                    break;
                case OperationType.Update:
                    TSFunctions function = new TSFunctions();
                    if (function.LoadByPrimaryKey(functionID.Value))
                    {
                        drpSystem.SelectedValue = function.SystemId.Value.ToString();
                        drpChannel.SystemID = function.SystemId.Value;
                        drpChannel.SelectedValue = function.ParentID.ToString();
                        txtCode.Text = function.FunctionCode;
                        txtFunctionName.Text = function.FunctionName;
                        txtURL.Text = function.FunctionURL;
                        txtSeq.Text = function.Sequence.ToString();
                        if (function.IsActive.Value)
                        {
                            radActive.Checked = true;
                        }
                        else
                        {
                            rad24.Checked = true;
                        }
                    }

                    btnSave.Text = "修改";
                    break;
                case OperationType.Delete:
                    btnSave_Click(null, null);
                    break;
            }
        }
    }



    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            TSFunctions function = new TSFunctions();
            switch (ActionType)
            {
                case OperationType.Add:
                    function.FunctionName = txtFunctionName.Text.Trim();
                    function.SystemId = decimal.Parse(drpSystem.SelectedValue);
                    function.ParentID = decimal.Parse(drpChannel.SelectedValue);
                    function.FunctionURL = txtURL.Text;
                    function.Sequence = txtSeq.IntValue;
                    function.FunctionCode = txtCode.Text;
                    function.IsActive = radActive.Checked;
                    function.CreatedBy = LoginUserInfo.CurrentUser.LoginName;
                    function.LastUpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                    function.Save();
                    break;
                case OperationType.Update:
                    function.LoadByPrimaryKey(functionID.Value);
                    function.FunctionName = txtFunctionName.Text.Trim();
                    function.SystemId = decimal.Parse(drpSystem.SelectedValue);
                    function.ParentID = decimal.Parse(drpChannel.SelectedValue);
                    function.Sequence = txtSeq.IntValue;
                    function.FunctionURL = txtURL.Text;
                    function.FunctionCode = txtCode.Text;
                    function.LastUpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                    function.LastUpdatedTime = DateTime.Now;
                    function.IsActive = radActive.Checked;
                    function.Save();
                    break;
                case OperationType.Delete:
                    function.LoadByPrimaryKey(functionID.Value);
                    function.MarkAsDeleted();
                    function.Save();
                    WebHelper.GotoParent();
                    break;
            }
            WebHelper.CloseDialog(true);
        }
        catch (Exception ex)
        {
            lbMessage.Text = ex.Message;
        }
    }

    protected void btnClose_Click(object sender, EventArgs e)
    {
        WebHelper.CloseDialog();
    }
}